/**
 ********************************************************************************
 *
 *文件名称：
 *
 *文件日期：
 *
 *文件描述：
 *
 *文件版本：
 *
 *修改历史：
 *
 ********************************************************************************
 ***/

/*******************************	头文件包含区	**********************************/
#include "module_control.h"
#include "at_cmd_tx.h"

#include "at_cmd_rx.h"

/******************************* 全局变量定义区 **********************************/
void (*my_tx_fun[FUN_POINT_BUF_SIZE])(void);
void (*my_rx_fun[FUN_POINT_BUF_SIZE])(void);
QUEUE_T my_sendQueue;
my_queue_struct my_queue_buff[QUEUE_SIZE];
deviceInfo device_info = {0};
global_control_struct global_control;
deviceTime device_time = {0};
uint8_t rx_wireless_flag = 0;
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
uint16_t crc16(uint8_t *Pushdata, uint16_t length)
{
	uint16_t Reg_CRC = 0xffff;
	uint16_t i, j;
	for (i = 0; i < length; i++)
	{
		Reg_CRC ^= (uint16_t)(*Pushdata++);
		for (j = 0; j < 8; j++)
		{
			if (Reg_CRC & 0x0001)
				Reg_CRC = Reg_CRC >> 1 ^ 0xA001;
			else
				Reg_CRC >>= 1;
		}
	}

	return Reg_CRC;
}

#if (PROJECT_TYPEDEF == NB)
const unsigned short crc16_table[256] = {
	0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
	0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
	0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
	0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
	0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
	0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
	0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
	0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
	0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
	0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
	0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
	0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
	0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
	0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
	0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
	0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
	0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
	0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
	0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
	0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
	0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
	0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
	0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
	0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
	0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
	0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
	0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
	0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
	0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
	0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
	0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
	0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0};

uint16_t do_crc(uint16_t reg_init, uint8_t *data, uint16_t length)
{
	uint16_t cnt;
	uint16_t crc_reg = reg_init;
	for (cnt = 0; cnt < length; cnt++)
	{
		crc_reg = (crc_reg >> 8) ^ crc16_table[(crc_reg ^ *(data++)) & 0xFF];
	}
	return crc_reg;
}

#endif
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
uint8_t crc_check(uint8_t *in_buf, uint16_t in_buf_len)
{
	volatile uint16_t crc, crc_buf;
	volatile uint8_t crc_h, crc_l;

	crc = crc16(in_buf, in_buf_len - 2);
	crc_h = in_buf[in_buf_len - 1];
	crc_l = in_buf[in_buf_len - 2];
	crc_buf = ((uint16_t)crc_h << 8) + crc_l;
	if (crc == crc_buf)
		return 1;
	else
		return 0;
}

/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
void com_rx_task(void)
{
	if (m_uart0.rx_len)
	{
		m_uart0.timeOut++;
		if (m_uart0.timeOut >= 3)
		{
			m_uart0.timeOut = 0;
			m_uart0.rx_ok = 1;
			pcUart.my_pc_readLen = m_uart0.rx_len;
			my_pc_recv();
			uart0_rx_dataReset();
		}
	}
	if (m_uart1.rx_len)
	{
		m_uart1.timeOut++;
		if (m_uart1.timeOut >= 3)
		{
			m_uart1.timeOut = 0;
			m_uart1.rx_ok = 1;
			LOGD("-->rx:\r\n-->len:%d\r\n", m_uart1.rx_len);
			my_usart_send_data1(mainUart.my_at_readbuf, m_uart1.rx_len);
			//处理这个接收字符串
			my_rx_fun[global_control.cmd_num]();
			uart1_rx_dataReset();
		}
	}
	if (m_uart2.rx_len)
	{
		m_uart2.timeOut++;
		if (m_uart2.timeOut >= 3)
		{
			m_uart2.timeOut = 0;
			m_uart2.rx_ok = 1;
			//LOGD("-->rx:%s\r\n-->len:%d\r\n", mainUart.my_at_readbuf, m_uart1.rx_len);
			//处理这个接收字符串
			bl0939_data_recive(pcUart.bl0939_rx_buf);
			uart2_rx_dataReset();
		}
	}
}

/**
 *********************************************************************************
 *
 *函数名称：static void my_cmd_deal(uint8_t cmd,void (*tx_fun)(void),void (*rx_fun)(void),uint16_t timeout)
 *
 *函数参数：
 *				cmd：命令序列号
 *				*tx_fun：发送函数指针
 *				*rx_fun：接收函数指针
 *				timeout：函数超时时间
 *
 *函数返回值：无
 *
 *功能描述：命令序列号初始化处理
 *
 *********************************************************************************
 **/
void my_cmd_deal(uint8_t cmd, void (*tx_fun)(void), void (*rx_fun)(void), uint32_t delay_time, uint32_t timeout, ACK_STATE ack_flag)
{
	my_queue_struct my_queue;

	if (queue_GetItemCount(&my_sendQueue) > QUEUE_SIZE)
		return;

	if (tx_fun == NULL || rx_fun == NULL)
	{
		//LOGD("ERROR:tx_fun or rx_funerror\r\n");
		return;
	}
	//LOGD("cmd :%d\r\n",cmd);
	/*	根据命令序列号在函数指针数组中初始化对应命令的函数指针	*/
	my_tx_fun[cmd] = tx_fun;
	my_rx_fun[cmd] = rx_fun;

	/*	向队列中添加命令序列号	*/
	my_queue.serial = cmd;
	my_queue.ack_en = ack_flag;
	my_queue.protocol = protocol;
	/*	记录该命令的超时时间和命令序列号	*/
	my_queue.cmd_delay_time = delay_time;
	my_queue.cmd_timeout = timeout;
	queue_Enqueue(&my_sendQueue, &my_queue);
}
/**
 *********************************************************************************
 *
 *函数名称：static void my_queue_init(void)
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：队列初始化
 *
 *********************************************************************************
 **/
void my_queue_init(void)
{
	if (DOWMLINK_READ >= QUEUE_SIZE)
	{
		// LOGD("ERROR:DOWMLINK_READ is greater than QUEUE_SIZE\r\n");
	}
	memset(my_queue_buff, 0, sizeof(my_queue_buff));
	queue_Init(&my_sendQueue, my_queue_buff, sizeof(my_queue_buff[0]), QUEUE_SIZE);
}
/**
 *********************************************************************************
 *
 *函数名称：void my_cmd_runing(void)
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：命令监控函数，监控队列中是否有需要处理的指令并处理
 *
 *********************************************************************************
 **/
extern uint8_t OTA_status_before;
void my_cmd_runing(void)
{
	my_queue_struct my_queue;
	global_control.cmd_tx_time++;
	/*	判断上一条指令是否应答成功	*/
	if (global_control.ack_ok != MY_SET)
	{
		if (global_control.cmd_tx_time >= global_control.cmd_timeout) //指令超时重启模组
		{
			global_control.cmd_tx_time = 0;
	//		LOGD("my_cmd_runing time out\r\n");
			my_cmd_init(); //重新初始化
		}
		if (global_control.error_code != 0) //指令错误重发
		{
		//	LOGD("----------- error_code:%d  \r\n", global_control.error_code);
			//LOGD();
			//global_control.error_code = 0;
			my_cmd_retx(&my_sendQueue);
		}
		return;
	}
	else
	{
		/*	判断队列是否为空	*/
		if (queue_GetItemCount(&my_sendQueue) == 0)
		{
			global_control.cmd_tx_time = 0;
			return;
		}
		
		/*  指令延时时间是否到达  */
		if (global_control.cmd_tx_time < global_control.cmd_delay_time)
		{
			return;
		}
		
		memset(main_tx.my_at_sendbuf, 0, sizeof(main_tx.my_at_sendbuf));
		memset(main_tx.my_at_data_str, 0, sizeof(main_tx.my_at_data_str));
		queue_Dequeue(&my_sendQueue, &my_queue);
		//		/*	发送函数	*/
		//		if (my_queue.serial >= QUEUE_SIZE)
		//		{
		//			LOGD("ERROR:my_queue.serial is error:%d\r\n", my_queue.serial);
		//			return;
		//		}
		global_control.cmd_tx_time = 0;
		global_control.cmd_num = my_queue.serial;
		global_control.cmd_timeout = my_queue.cmd_timeout;
		global_control.cmd_delay_time = my_queue.cmd_delay_time;
		global_control.protocol = my_queue.protocol;
		if (my_queue.ack_en == ACK) //需要重发
			global_control.ack_ok = MY_RESET;
		else //不需要重发
			global_control.ack_ok = MY_SET;
		my_tx_fun[my_queue.serial]();
#if (PROJECT_TYPEDEF == NB || PROJECT_TYPEDEF == NB_ONENET)
		if (global_control.cmd_num >= AT_RESTART)
#else
		if (global_control.cmd_num > AT_RESTART)
#endif
		{
			my_usart_send_data(main_tx.my_at_sendbuf, strlen((char *)main_tx.my_at_sendbuf));
		}

		if(OTA_status_before != 0xff)
		{
#if (PROJECT_TYPEDEF == NB_ONENET)
			if(global_control.cmd_num != AT_QMTOPEN)
#endif
			{
				reg.RW.ota_flag = OTA_status_before;
				OTA_status_before = 0xff;
			}

		}
	}
}

/**
 *********************************************************************************
 *
 *函数名称：void my_cmd_init(void)
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：指令初始化
 *
 *********************************************************************************
 **/
void my_cmd_init(void)
{
	my_queue_init();
	reg.W.flash_data = 0;
	device_info.QmtSubFlag = 0;
#if (PROJECT_TYPEDEF == NB)
	static uint8_t clear_nb_flag = 0;
	if (++clear_nb_flag >= 5)
	{
		clear_nb_flag = 0;
		reg.RW.reset_process = 0;
		data_flash_write();
	}

	if (reg.RW.ota_flag != NONE_OTA && reg.RW.ota_flag != OTA_PACKAGE_RX_OK) //表明此刻运行在boot区
	{
		reg.RW.ota_flag = IS_OTA;
	}
	if (global_control.error_code == AT_CREG)
	{
		my_cmd_deal(AT_CFUN0, AT_TX_CFUN0, AT_RX_Universal, 1, 2000, ACK);
		my_cmd_deal(AR_NCSEARFCN, AT_TX_NCSEARFCN, AT_RX_Universal, 10, 2000, ACK);
		my_cmd_deal(AT_CFUN1, AT_TX_CFUN1, AT_RX_Universal, 1, 2000, ACK);
	}
	if (reg.RW.reset_process == 16)
	{
		my_cmd_deal(AT_RESTART, AT_TX_Restart, AT_RX_Restart, 900, 2000, ACK); //超时时间20s
	}
	else
	{
		uart_switch_baud(USART_BPS_9600);
		my_cmd_deal(AT_RESTART, AT_TX_Restart, AT_RX_Restart, 900, 2000, NO_ACK); //超时时间20s
		my_cmd_deal(AT_QMTCFG_0, AT_TX_SWITCH_BAUD, AT_RX_SWITCH_BAUD, 50, 2000, NO_ACK);
	}
	my_cmd_deal(AT_ATI, AT_TX_ATI, AT_RX_Universal, 100, 2000, ACK);
	my_cmd_deal(AT_IMEI, AT_TX_IMEI, AT_RX_IMEI, 250, 2000, ACK);
	my_cmd_deal(AT_QCCID, AT_TX_QCCID, AT_RX_QCCID, 150, 2000, ACK);
	my_cmd_deal(AT_QCFG, AT_TX_QCFG, AT_RX_Universal, 100, 2000, ACK);

	if (reg.RW.reset_process == 16)
	{
		// my_cmd_deal(WAITING, send_waiting_to_receive, waiting_to_receive, 400, 2000,NO_ACK);//查询卡的状态，同时转为接收模式等待接
		my_cmd_deal(AT_CREG, AT_TX_CREG, AT_RX_CREG, 10, 2000, ACK);
		my_cmd_deal(AT_CSQ, AT_TX_CSQ, waiting_to_receive, 500, 2000, ACK);
		my_cmd_deal(AT_CGATT_GET, AT_TX_CGATT_GET, AT_RX_CGATT, 200, 2000, ACK);
		my_cmd_deal(AT_CCLK, AT_TX_CCLK, waiting_to_receive, 1, 2000, ACK);
		my_cmd_deal(AT_NMSTATUS, AT_TX_NMSTATUS, AT_RX_NMSTATUS, 1, 2000, ACK);
		my_cmd_deal(WAITING, send_waiting_to_receive, waiting_to_receive, 1, 2000, NO_ACK); //查询卡的状态，同时转为接收模式等待接
	}
	else
	{
		my_cmd_deal(AT_QREGSWT, AT_TX_QREGSWT, AT_RX_Universal, 1, 2000, ACK);
		my_cmd_deal(AT_NCDP, AT_TX_NCDP, AT_RX_Universal, 10, 2000, ACK);
		my_cmd_deal(AT_CGDCONT, AT_TX_CGDCONT, AT_RX_Universal, 10, 2000, ACK);
		my_cmd_deal(AT_CPSMS, AT_TX_CPSMS, AT_RX_Universal, 10, 2000, ACK);
		my_cmd_deal(AT_CEDRXS, AT_TX_CEDRXS, AT_RX_CEDRXS, 10, 2000, ACK);
	}
#elif (PROJECT_TYPEDEF == NB_ONENET)
	static uint8_t clear_nb_flag = 0;
	if (++clear_nb_flag >= 5)
	{
		clear_nb_flag = 0;
		reg.RW.reset_process = 0;
		data_flash_write();
	}

	if (reg.RW.ota_flag != NONE_OTA && reg.RW.ota_flag != OTA_PACKAGE_RX_OK) //表明此刻运行在boot区
	{
		reg.RW.ota_flag = IS_OTA;
	}
	if (global_control.error_code == AT_CREG)
	{
		my_cmd_deal(AT_CFUN0, AT_TX_CFUN0, AT_RX_Universal, 1, 2000, ACK);
		my_cmd_deal(AR_NCSEARFCN, AT_TX_NCSEARFCN, AT_RX_Universal, 10, 2000, ACK);
		my_cmd_deal(AT_CFUN1, AT_TX_CFUN1, AT_RX_Universal, 1, 2000, ACK);
	}
	if (reg.RW.reset_process == 16)
	{
		my_cmd_deal(AT_RESTART, AT_TX_Restart, AT_RX_Restart, 900, 2000, ACK); //超时时间20s
	}
	else
	{
		uart_switch_baud(USART_BPS_9600);
		my_cmd_deal(AT_RESTART, AT_TX_Restart, AT_RX_Restart, 900, 2000, NO_ACK); //超时时间20s
		my_cmd_deal(AT_QMTCFG_0, AT_TX_SWITCH_BAUD, AT_RX_SWITCH_BAUD, 50, 2000, NO_ACK);
	}
	my_cmd_deal(AT_ATI, AT_TX_ATI, AT_RX_Universal, 100, 2000, ACK);
	my_cmd_deal(AT_IMEI, AT_TX_IMEI, AT_RX_IMEI, 250, 2000, ACK);
	my_cmd_deal(AT_QCCID, AT_TX_QCCID, AT_RX_QCCID, 150, 2000, ACK);
	my_cmd_deal(AT_CPSMS, AT_TX_CPSMS, AT_RX_Universal, 10, 2000, ACK);

	if (reg.RW.reset_process == 16)
	{
		// my_cmd_deal(WAITING, send_waiting_to_receive, waiting_to_receive, 400, 2000,NO_ACK);//查询卡的状态，同时转为接收模式等待接
		my_cmd_deal(AT_CSQ, AT_TX_CSQ, waiting_to_receive, 500, 2000, ACK);
		my_cmd_deal(AT_CREG, AT_TX_CREG, AT_RX_CREG, 10, 2000, ACK);
		my_cmd_deal(AT_CGATT_GET, AT_TX_CGATT_GET, AT_RX_CGATT, 200, 2000, ACK);
		my_cmd_deal(AT_CCLK, AT_TX_CCLK, waiting_to_receive, 1, 2000, ACK);
		my_cmd_deal(AT_QCFG, AT_TX_QCFG, waiting_to_receive, 100, 2000, ACK);
		my_cmd_deal(AT_MIPLCONFIG, AT_TX_MIPLCONFIG, waiting_to_receive, 100, 2000, ACK);
		my_cmd_deal(AT_QMTCFG_0, AT_TX_QMTCFG_0, waiting_to_receive, 1, 2000, ACK);
		my_cmd_deal(AT_QMTCFG_1, AT_TX_QMTCFG_1, waiting_to_receive, 1, 2000, ACK);

		my_cmd_deal(AT_QMTCFG_2, AT_TX_QMTCFG_2, delay_MIPLDISCOVER, 1, 2000, ACK);

		my_cmd_deal(WAITING, send_waiting_to_receive, waiting_to_receive, 1, 2000, NO_ACK); //查询卡的状态，同时转为接收模式等待接
	}
	else
	{
		my_cmd_deal(AT_OPEN_LED, AT_TX_OPENLED, waiting_to_receive, 10, 2000, ACK);
		my_cmd_deal(AT_QREGSWT, AT_TX_QREGSWT, AT_RX_Universal, 1, 2000, ACK);
		// my_cmd_deal(AT_NCDP, AT_TX_NCDP, AT_RX_Universal, 10, 2000, ACK);
		my_cmd_deal(AT_CGDCONT, AT_TX_CGDCONT, AT_RX_Universal, 10, 2000, ACK);
		my_cmd_deal(AT_CEDRXS, AT_TX_CEDRXS, AT_RX_CEDRXS, 10, 2000, ACK);
	}
	// my_cmd_deal(WAITING, send_waiting_to_receive, waiting_to_receive, 1, 2000,NO_ACK);//查询卡的状态，同时转为接收模式等待接收
#elif (PROJECT_TYPEDEF == CAT1)
	if (reg.RW.ota_flag != NONE_OTA) //表明此刻运行在boot区
	{
		reg.RW.ota_flag = IS_OTA;
	}
	my_cmd_deal(AT_RESTART, AT_TX_Restart, AT_RX_Restart, 200, 3000, ACK); //超时时间20s
	my_cmd_deal(AT_ATI, AT_TX_ATI, AT_RX_Universal, 200, 3000, ACK);
	my_cmd_deal(AT_ATE0, AT_TX_ATE0, AT_RX_Universal, 100, 3000, ACK);
	my_cmd_deal(AT_IMEI, AT_TX_IMEI, AT_RX_IMEI, 10, 3000, ACK);
	my_cmd_deal(AT_CPIN, AT_TX_CPIN, AT_RX_Universal, 10, 3000, ACK);
	my_cmd_deal(AT_QCCID, AT_TX_QCCID, AT_RX_QCCID, 10, 3000, ACK);
	my_cmd_deal(AT_CSQ, AT_TX_CSQ, waiting_to_receive, 10, 3000, ACK);
	my_cmd_deal(AT_CREG, AT_TX_CREG, AT_RX_CREG, 10, 3000, ACK);
	my_cmd_deal(AT_CGATT_GET, AT_TX_CGATT_GET, AT_RX_CGATT, 1, 3000, ACK);
	//my_cmd_deal(AT_CCLK, AT_TX_CCLK, AT_RX_CCLK, 1, 3000, ACK);
	my_cmd_deal(AT_CCLK, AT_TX_CCLK, waiting_to_receive, 1, 50000, ACK);
	my_cmd_deal(AT_QMTCFG_0, AT_TX_QMTCFG_0, AT_RX_Universal, 1, 3000, ACK);
	my_cmd_deal(AT_QMTCFG_1, AT_TX_QMTCFG_1, AT_RX_Universal, 1, 3000, ACK);
	//my_cmd_deal(AT_QMTCFG_2, AT_TX_QMTCFG_2, AT_RX_Universal, 1, 3000, ACK);
	my_cmd_deal(AT_QMTCFG_3, AT_TX_QMTCFG_3, AT_RX_Universal, 10, 3000, ACK);
	my_cmd_deal(AT_QMTOPEN, AT_TX_QMTOPEN, AT_RX_QMTOPEN, 10, 3000, ACK);
	my_cmd_deal(AT_QMTCONN, AT_TX_QMTCONN, AT_RX_QMTCONN, 10, 3000, ACK);
	my_cmd_deal(AT_QMTSUB, AT_TX_QMTSUB, AT_RX_QMTSUB, 10, 3000, ACK);
	my_cmd_deal(WAITING, send_waiting_to_receive, waiting_to_receive, 1, 3000, NO_ACK); //查询卡的状态，同时转为接收模式等待接收
#endif
	global_control.ack_ok = MY_SET;
	global_control.error_code = AT_DEFAULT;
	global_control.cmd_num = AT_RESTART;
	global_control.error_code_cnt = 0;
	global_control.cmd_timeout = 3000;
	global_control.cmd_delay_time = 200;

#if (PROJECT_TYPEDEF == NB)
	device_info.mqtt_state = NB_UINIT;
#elif (PROJECT_TYPEDEF == NB_ONENET)
	device_info.mqtt_state = NB_UINIT;
#elif (PROJECT_TYPEDEF == CAT1)
	device_info.mqtt_state = 0;
#endif
}
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
//;
void data_report(void)
{
//	static u8  run_count_clear_flag = 0;//, power_on_flag = 0;
	//static u32 power_on_time = 0;
	
	/*  注册上线  */
#if (PROJECT_TYPEDEF == NB)
	if (NB_LWM2M_OK != device_info.mqtt_state || reg.RW.ota_flag == OTA_READY)
#elif (PROJECT_TYPEDEF == NB_ONENET)
	if (NB_LWM2M_OK != device_info.mqtt_state)
#else
	if (!device_info.mqtt_state || reg.RW.ota_flag == OTA_READY)
#endif
		return;

	device_time.lamp_data_report_time++;
	device_time.else_data_repota_time++;
	device_time.at_time++;
	device_time.run_data_report_time++;
	device_time.first_data++;
	
	//定时查询信号值和时间
	if (device_time.at_time >= 30000 * 12)
	{
		device_time.at_time = 0;
#if (PROJECT_TYPEDEF == CAT1)
		restore_factory_cclk_way();
#endif
		my_cmd_deal(AT_CSQ, AT_TX_CSQ, waiting_to_receive, 1, 2000, ACK);
		my_cmd_deal(AT_CCLK, AT_TX_CCLK, waiting_to_receive, 1, 50000, ACK);
	}
	if(!reg.W.heartbeat_interval)
	{
		reg.W.heartbeat_interval = 1800;
	}
	
	//路灯数据上报
#if (PROJECT_TYPEDEF == NB_ONENET || PROJECT_TYPEDEF == NB)
	//if (device_time.lamp_data_report_time >= 120000 + device_info.random) //十分钟加随机数
	if (device_time.lamp_data_report_time >=  ((u32)reg.W.heartbeat_interval * 100) + device_info.random) //十分钟加随机数
#else
	if (device_time.lamp_data_report_time >= 60000 + device_info.random) //十分钟加随机数
#endif
	{
		device_time.lamp_data_report_time = 0;
		protocol.reg_num = 2;
		protocol.reg_addr[0] = 0x1009;
#if (LED_TYPEDEF == DOUBLE_LED)
		protocol.reg_len[0] = 2;
#else
		protocol.reg_len[0] = 1;
		protocol.reg_addr[1] = 0x3058;
		protocol.reg_len[1] = 1;
#endif
#if (PROJECT_TYPEDEF == CAT1)
		my_cmd_deal(AT_QMTPUB_UPLINK, AT_TX_UPLINK10, AT_RX_UPLINK, 1, 1000, ACK);
	//	LOGD("test04:AT_TX_UPLINK10\r\n");
#endif
		my_cmd_deal(AT_QMTPUB_UP_LAMP, AT_TX_QMTPUB_UP_LAMP, waiting_to_receive, 1, 2000, ACK);
#if (PROJECT_TYPEDEF == NB_ONENET)
		my_cmd_deal(AT_QMTCONN, AT_TX_QMTCONN, waiting_to_receive, 10, 2000, ACK);
#endif
	}
	//其他数据上报
	if(reg.W.data_tx_time <= 600)
		reg.W.data_tx_time = 600;
	if (device_time.else_data_repota_time >= ((u32)reg.W.data_tx_time * 100) + device_info.random) //一小时加随机数
	{
		device_time.else_data_repota_time = 0;
		protocol.reg_num = 4;
		protocol.reg_addr[0] = 0x302e;
		protocol.reg_len[0] = 3;
		protocol.reg_addr[1] = 0x3002;

		protocol.reg_len[1] = 9;

		protocol.reg_addr[2] = 0x3010;
		protocol.reg_len[2] = 2;
		protocol.reg_addr[3] = 0x1009;
		protocol.reg_len[3] = 1;
#if (PROJECT_TYPEDEF == CAT1)
		my_cmd_deal(AT_QMTPUB_UPLINK, AT_TX_UPLINK10, AT_RX_UPLINK, 1, 1000, ACK);
		LOGD("test05:AT_TX_UPLINK10\r\n");
#endif
		my_cmd_deal(AT_QMTPUB_UP_LAMP, AT_TX_QMTPUB_UP_LAMP, waiting_to_receive, 1, 2000, ACK);

	}
	// 12小时上报一次运行时间
	if (device_time.run_data_report_time >= 4320000 + device_info.random)
	{
		device_time.run_data_report_time = 0;
		protocol.reg_num = 1;
		protocol.reg_addr[0] = 0x3012;

		protocol.reg_len[0] = 8;
#if (PROJECT_TYPEDEF == CAT1)
		my_cmd_deal(AT_QMTPUB_UPLINK, AT_TX_UPLINK10, AT_RX_UPLINK, 1, 1000, ACK);
		LOGD("test06:AT_TX_UPLINK10\r\n");
#endif
		my_cmd_deal(AT_QMTPUB_UP_LAMP, AT_TX_QMTPUB_UP_LAMP, waiting_to_receive, 1, 2000, ACK);
	}

	//上线的第一包数据
	if (device_info.QmtSubFlag == 1 && device_time.first_data >= device_info.random)
	{
		device_info.QmtSubFlag = 0;
		protocol.reg_num = 3;
		protocol.reg_addr[0] = 0x1009;

		protocol.reg_len[0] = 1;

		protocol.reg_addr[1] = 0x3002;

		protocol.reg_len[1] = 9;

		protocol.reg_addr[2] = 0x3010;
		protocol.reg_len[2] = 10;
#if (PROJECT_TYPEDEF == CAT1)
		my_cmd_deal(AT_QMTPUB_UPLINK, AT_TX_UPLINK10, AT_RX_UPLINK, 1, 1000, ACK);
		LOGD("test07:AT_TX_UPLINK10\r\n");
#endif
		my_cmd_deal(AT_QMTPUB_UP_LAMP, AT_TX_QMTPUB_UP_LAMP, waiting_to_receive, 1, 2000, ACK);

		protocol.reg_num = 3;
		protocol.reg_addr[0] = 0x3023;
		protocol.reg_len[0] = 5;
		protocol.reg_addr[1] = 0x302B;
		protocol.reg_len[1] = 6;
		protocol.reg_addr[2] = 0x3042;
		protocol.reg_len[2] = 15;
#if (PROJECT_TYPEDEF == CAT1)
		my_cmd_deal(AT_QMTPUB_UPLINK, AT_TX_UPLINK10, AT_RX_UPLINK, 1, 1000, ACK);
		LOGD("test08:AT_TX_UPLINK10\r\n");
#endif
		my_cmd_deal(AT_QMTPUB_UP_LAMP, AT_TX_QMTPUB_UP_LAMP, waiting_to_receive, 1, 2000, ACK);

	}
	check_lamp_sw(reg.RW.lamp[0].sw,reg.RW.lamp[1].sw, leakDete.system_run_timestamp);
}
/**
 *********************************************************************************
 *
 *函数名称：static void my_cmd_retx(QUEUE_T * pQueue)
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：指令重发
 *
 *********************************************************************************
 **/
#if (PROJECT_TYPEDEF == NB_ONENET || PROJECT_TYPEDEF == NB)
u8 error_cnt = 10;
#endif
static void my_cmd_retx(QUEUE_T *pQueue)
{
	static uint32_t retx_time = 0, last_error_code = 0;

	if (retx_time < 300) //重发间隔
	{
		retx_time++;
		return;
	}
	retx_time = 0;
	if (last_error_code != global_control.error_code)
	{
		last_error_code = global_control.error_code;
		global_control.error_code_cnt = 0;
	}
	else
	{
		if (global_control.error_code == AT_CCLK)
		{
			global_control.ack_ok = 1;
#if (PROJECT_TYPEDEF == CAT1)
			get_cclk_another_way();
#endif
		}
		global_control.error_code_cnt++;

#if (PROJECT_TYPEDEF == NB_ONENET || PROJECT_TYPEDEF == NB)
		if (global_control.error_code == AT_CREG || global_control.error_code == AT_CSQ)
		{
			error_cnt = 20;
		}
		else
		{
			error_cnt = 10;
		}
		if (global_control.error_code_cnt >= error_cnt) //错误重发5次
#else

		if (global_control.error_code_cnt >= 10) //错误重发5次
#endif
		{
#if (PROJECT_TYPEDEF == CAT1)
			if (global_control.error_code == AT_QMTOPEN)
			{
				if(((0 == reg.W.ip_h) && (0 == reg.W.ip_l) && (0 == reg.W.ip_port)) || ((0xFFFF == reg.W.ip_h) && (0xFFFF == reg.W.ip_l) && (0xFFFF == reg.W.ip_port)))
				{
					
				}
				else
				{
					reg.W.ip_h = 0;
					reg.W.ip_l = 0;
					reg.W.ip_port = 0;
					data_flash_write();
				}
			}
#endif
			LOGD("my_cmd_retx \r\n");
			my_cmd_init(); //重新初始化
			return;
		}
	}

	pQueue->out = (pQueue->out - 1) % pQueue->queueSize;
	pQueue->count = pQueue->count + 1;

	global_control.ack_ok = MY_SET;
}
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：接收处理函数
 *
 *********************************************************************************
 **/

/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
